import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_give/common_ui/navigation_bar_widget.dart';
import 'package:flutter_give/pages/Home/home_page.dart';
import 'package:flutter_give/pages/hot_key/hot_key_page.dart';
import 'package:flutter_give/pages/personal/personal_page.dart';
import '../utils/constants.dart';
import '../utils/sp_utils.dart';

class TabPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TabPageState();
  }
}

class _TabPageState extends State<TabPage> {
  // 接口实例数据
  // HotKeyViewModel viewModel = HotKeyViewModel();
  // final GlobalKey<HotKeyPageState> hotKeyPageKey = GlobalKey<HotKeyPageState>();

  // late-延迟初始化
  // 页面下标
  int currentIndex = 0;

  // 页面数组
  late List<Widget> pages;

  // 底部标题
  late List<String> labels;

  // 导航栏的icon数组：切换前
  late List<Widget> icons;

  // 导航栏的icon数组：切换后
  late List<Widget> activeIcons;

  void initTabData() {
    pages = [
      HomePage(key: HomePageState.globalKeytwo),
      HotKeyPage(key: HotKeyPageState.globalKey),
      // HomePagetg(),
      PersonalPage(key: PersonalPageState.globalKeythere)
    ];
    // labels = ["首页", "消息", "消息", "我的"];
    labels = ["首页", "消息", "我的"];
    icons = [
      Image.asset(
        "assets/Tab/video.png",
        width: 32,
        height: 32,
      ),
      Image.asset(
        "assets/Tab/msg.png",
        width: 32,
        height: 32,
      ),
      // Image.asset(
      //   "assets/Tab/msg.png",
      //   width: 32,
      //   height: 32,
      // ),
      Image.asset(
        "assets/Tab/mini.png",
        width: 32,
        height: 32,
      ),
    ];
    activeIcons = [
      Image.asset(
        "assets/Tab/video_pitch.png",
        width: 32,
        height: 32,
      ),
      Image.asset(
        "assets/Tab/msg_pitch.png",
        width: 32,
        height: 32,
      ),
      // Image.asset(
      //   "assets/Tab/msg_pitch.png",
      //   width: 32,
      //   height: 32,
      // ),
      Image.asset(
        "assets/Tab/mini_pitch.png",
        width: 32,
        height: 32,
      ),
    ];
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initTabData();
  }

  @override
  Widget build(BuildContext context) {
    return NavigationBarWidget(
        pages: pages,
        labels: labels,
        icons: icons,
        activeIcons: activeIcons,
        currentIndex: 0,
        onTabChange: (index) {
          print(index);
          // 切换页面重新加载方法
          SpUtils.saveString(Constants.SP_ZHSL_TABINDEX, "${index}");
          if (index == 0) {
            // 调用当前页面reloadData方法 - 进行重载
            HomePageState.globalKeytwo.currentState?.reloadData();
          } else if (index == 1) {
            HotKeyPageState.globalKey.currentState?.reloadData();
          } else if (index == 2) {
            PersonalPageState.globalKeythere.currentState?.reloadData();
          }
        },
        onTabReload: (index) {
          // 调用当前页面的 reloadData 方法
        });
  }
}
